﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Text;

namespace Ys.Base.Common
{
    /// <summary>
    /// 返回分页结果类型实体
    /// </summary>
  public  class ReturnPagedResult<T>:ReturnResult<List<T>>
    {

        private long _pageindex = 0;
        private long _PageSize = 0;
        /// <summary>
        /// 总记录数
        /// </summary>
        public long Count { get; set; } = 0;
        /// <summary>
        /// 当前页
        /// </summary>
        public long PageIndex
        {
            get { return _pageindex; }
            set
            {
                if (value > PageCount)
                {
                    _pageindex = PageCount;
                }
                else
                {
                    _pageindex = value;
                }
            }
        }
        /// <summary>
        /// 页面大小
        /// </summary>
        public long Limit {
            get { return _PageSize; }
            set { _PageSize = value; }
        }
        /// <summary>
        /// 回写page数据
        /// </summary>
        /// <param name="page"></param>
        /// <param name="Count"></param>
        public void Pageset(PageEntity page,long Count) {
            this.Count = Count;
            this.Limit = page.Limit;
            this.PageIndex = page.Page;
        }
        /// <summary>
        /// 回写page数据
        /// </summary>
        /// <param name="Limit"></param>
        /// <param name="Page"></param>
        /// <param name="Count"></param>
        public void Pageset(long Limit,long Page, long Count)
        {
            this.Count = Count;
            this.Limit = Limit;
            this.PageIndex = Page;
        }

        /// <summary>
        /// 总页数
        /// </summary>
        public long PageCount { get { return Count.CeilingDivide(Limit); } }


        /// <summary>
        /// 构造
        /// </summary>
        public ReturnPagedResult()
        {
            Data = new List<T>();
        }


        /// <summary>
        /// 创建page返回结果数据
        /// </summary>
        /// <param name="data">绑定的数据</param>
        /// <param name="count">总数据量</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">单页大小</param>
        public ReturnPagedResult(List<T> data,int count,int pageIndex, int pageSize)
        {
            if (data == null)
            {
                Status = false;
                Code = (int)HttpStatusCode.BadRequest;
            }
            else
            {
                Status = true;
                Code = (int)HttpStatusCode.OK;

            }
            this.Data = data;
            Count = count;
            this.Limit = _PageSize;
            this.PageIndex = pageIndex;
        }

    }
}
